package mi.payment.admin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import lainey.ui.dialog.IconHintDialog;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.admin.pay88.IPay88MainActivity;
import mi.payment.constants.Params;
import mi.payment.ui.admin.CommunicationParameterSettingActivity;
import mi.payment.ui.admin.OtherSettingsActivity;
import mi.payment.ui.admin.PasswordManagementActivity;
import mi.payment.ui.admin.SystemParameterSettingsActivity;
import mi.payment.ui.admin.TerminalKeyManagementActivity;
import mi.payment.ui.admin.TransactionManageSettingsActivity;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.other.LoginActivity;
import sunmi.payment.L3.L3BroadcastHelper;

/**
 * 系统操作员首页
 */
public class SystemUserHomePageActivity extends ParentActivity {

    private IconHintDialog mIconHintDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_user);
        initView();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.admin_system_operator);
        titleView.setLeftImageVisibility(View.GONE);
        titleView.setRightImageVisibility(View.VISIBLE);
        titleView.setRightImageResource(R.drawable.ui_ic_logout);
        titleView.setRightImageOnClickListener(
                v -> onBackPressed()
        );

        // 商户管理设置
        View item = findViewById(R.id.item_merchant);
        TextView itemText = item.findViewById(R.id.tv_type);
        ImageView itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.merchant_setting);
        itemImage.setImageResource(R.drawable.setting_merchant_management);

        // 交易管理设置
        item = findViewById(R.id.item_transaction);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.trade_setting);
        itemImage.setImageResource(R.drawable.setting_transaction_management);

        // 通讯参数设置
        item = findViewById(R.id.item_communication);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.communication_setting);
        itemImage.setImageResource(R.drawable.setting_communication_parameter);

        // 系统参数设置
        item = findViewById(R.id.item_system_params);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.system_setting);
        itemImage.setImageResource(R.drawable.setting_download_management);

        // 密码修改
        item = findViewById(R.id.item_password);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.password_setting);
        itemImage.setImageResource(R.drawable.setting_password_modification);

        // 终端密钥管理
        item = findViewById(R.id.item_key);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.key_setting);
        itemImage.setImageResource(R.drawable.setting_terminal_key_management);

        // 其他管理设置
        item = findViewById(R.id.item_other);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.other_setting);
        itemImage.setImageResource(R.drawable.setting_system);

        item = findViewById(R.id.item_pay88);
        itemText = item.findViewById(R.id.tv_type);
        itemImage = item.findViewById(R.id.iv_icon);
        item.setOnClickListener(this);
        itemText.setText(R.string.pay88_setting);
        itemImage.setImageResource(R.drawable.setting_merchant_management);
    }

    @Override
    public void onBackPressed() {
        clickExit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissIconHintDialog();
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.item_merchant:
                openActivity(MerchantManagementSettingsActivity.class);
                break;
            case R.id.item_transaction:
                openActivity(TransactionManageSettingsActivity.class);
                break;
            case R.id.item_communication:
                openActivity(CommunicationParameterSettingActivity.class);
                break;
            case R.id.item_system_params:
                openActivity(SystemParameterSettingsActivity.class);
                break;
            case R.id.item_password:
                openActivity(PasswordManagementActivity.class);
                break;
            case R.id.item_key:
                openActivity(TerminalKeyManagementActivity.class);
                break;
            case R.id.item_other:
                openActivity(OtherSettingsActivity.class);
                break;
            case R.id.item_pay88:
                openActivity(IPay88MainActivity.class);
                break;
        }
    }

    private void clickExit() {
        if (Params.sL3Params != null) {
            L3BroadcastHelper.sendSuccessBroadcast(this);
            finish();
        } else {
            showIconHintDialog();
        }
    }

    private void showIconHintDialog() {
        if (mIconHintDialog == null) {
            mIconHintDialog = new IconHintDialog(this);
            mIconHintDialog.setRightText(R.string.ok);
            mIconHintDialog.setLeftText(R.string.cancel);
            mIconHintDialog.setMessage(R.string.admin_dialog_exit);
            mIconHintDialog.setIconResource(R.drawable.state_warning);
            mIconHintDialog.setLeftOnClickListener(
                    v -> dismissIconHintDialog()
            );
            mIconHintDialog.setRightOnClickListener(
                    v -> {
                        dismissIconHintDialog();
                        openActivity(LoginActivity.class);
                        finish();
                    }
            );
            mIconHintDialog.setCancelable(true);
            mIconHintDialog.setCanceledOnTouchOutside(true);
        }
        try {
            mIconHintDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void dismissIconHintDialog() {
        if (mIconHintDialog != null) {
            try {
                mIconHintDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, SystemUserHomePageActivity.class);
        context.startActivity(intent);
    }


}
